package com.ctsi.findexpert.app.rest;

import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.response.Page;
import com.ctsi.findexpert.dto.AppExpertDTO;
import com.ctsi.findexpert.dto.FindExpertDTO;
import com.ctsi.findexpert.entity.Expert;
import com.ctsi.findexpert.service.FindExpertCommonService;
import com.ctsi.findexpert.service.FindExpertService;
import com.ctsi.findexpert.vo.AddExpertVO;
import com.ctsi.findexpert.vo.UpdateExpertVO;
import com.ctsi.framework.common.response.UnifiedResponse;
import com.ctsi.framework.security.util.SecurityUtil;
import com.ctsi.organise.common.api.PersonServiceProxy;
import com.ctsi.organise.common.api.UserLoginServiceProxy;
import com.ctsi.organise.common.model.UserLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
public class FindExpertController {

    @Autowired
    private FindExpertCommonService findExpertCommonService;

    @Autowired
    private PersonServiceProxy personService;

    @Autowired
    private UserLoginServiceProxy userLoginService;

    @Autowired
    private FindExpertService findExpertService;

    /**
     * 添加专家
     * @param addExpertVO
     * @return
     */
    @PostMapping(value = "/findexpert/insert")
    public UnifiedResponse<Void> addExpert(@RequestBody AddExpertVO addExpertVO){
        String curuserId = SecurityUtil.getUser().getUserId();
//        Expert temp = findExpertCommonService.findrById(addExpertVO.getExpertId());
        // TODO: 验证是否已经存在
        Expert expert = new Expert();
        expert.setId(UUID.randomUUID().toString());
        expert.setExpertId(addExpertVO.getExpertId());
        expert.setCapability(addExpertVO.getCapability());
        expert.setRemarks(addExpertVO.getRemarks());
        UserLogin userLogin = userLoginService.findById(SecurityUtil.getUser().getLoginId());
        expert.setHxId(userLogin.getLoginName());
        expert.setActived("true");
        expert.setCreateBy(curuserId);
        expert.setCreateAt(new Date());
        findExpertCommonService.insert(expert,addExpertVO.getMajorCode());
        return UnifiedResponse.ok();
    }

    /**
     * 查询专家列表
     * @param expertNameLike
     * @param major
     * @param start
     * @param pageSize
     * @return
     */
    @PostMapping(value = "/findexpert/query")
    public UnifiedResponse<Page<FindExpertDTO>> queryExpertPage(@RequestParam(value = "expertNameLike",required = false) String expertNameLike,
                                                                @RequestParam(value = "major",required = false) String major,
                                                                @RequestParam(value = "start") Integer start,
                                                                @RequestParam(value = "pageSize",required = false,defaultValue = "15") Integer pageSize){
        Map<String,Object> params = new HashMap<>();
        if (UtilValidate.isNotEmpty(expertNameLike)) {
            expertNameLike = "%"+expertNameLike+"%";
            params.put("expertNameLike",expertNameLike);
        }
        if (UtilValidate.isNotEmpty(major)) {
            params.put("major",major);
        }
        String curuserId = SecurityUtil.getUser().getLoginId();
        Page<FindExpertDTO> page = findExpertCommonService.findByMulitCondition(params,curuserId,start,pageSize);
        page.getResult().stream().forEach(findExpertDTO -> {
            String expertId = findExpertDTO.getExpertId();
            findExpertDTO.setOrganName(personService.getPesonAsc(expertId).stream().map(o -> o.getPartyName()).collect(Collectors.joining(">")));
        });
        return UnifiedResponse.ok(page);
    }

    @PostMapping(value = "/findexpert/remove")
    public UnifiedResponse<Void> remove(@RequestParam(value = "ids") String ids) {
        String[] _ids = ids.split(",");
        Stream.of(_ids).forEach(id -> findExpertCommonService.remove(id));
        return UnifiedResponse.ok();
    }

    @GetMapping(value = "/findexpert/expert/{id}")
    public UnifiedResponse<UpdateExpertVO> findById(@PathVariable(value = "id") String id){
        UpdateExpertVO expert = findExpertCommonService.findUpdateExpert(id);
        return UnifiedResponse.ok(expert);
    }

    @PostMapping(value = "/findexpert/update")
    public UnifiedResponse<Void> update(@RequestBody UpdateExpertVO vo){
        Expert expert = new Expert();
        expert.setId(vo.getId());
        expert.setCapability(vo.getCapability());
        expert.setRemarks(vo.getRemarks());
        findExpertCommonService.updateByPKSelective(expert, vo.getMajorCode());
        return UnifiedResponse.ok();
    }

    /**
     * App 查询专家信息
     * @param focus
     * @param expertNameLike
     * @param start
     * @param pageSize
     * @return
     */
    @PostMapping(value = "/findexpert/findAppExpert")
    public UnifiedResponse<Page<AppExpertDTO>> findAppExpertDTOPage(@RequestParam(value = "focus",required = false) String focus,
                                                                    @RequestParam(value = "expertNameLike",required = false) String expertNameLike,
                                                                    @RequestParam(value = "start") Integer start,
                                                                    @RequestParam(value = "pageSize",required = false,defaultValue = "15") Integer pageSize){
        Map<String,Object> params = new HashMap<>();
        String userId = SecurityUtil.getUser().getUserId();
        params.put("userId",userId);
        if (UtilValidate.isNotEmpty(focus)) {
            params.put("focus",Integer.parseInt(focus));
        }
        if (UtilValidate.isNotEmpty(expertNameLike)) {
            params.put("expertNameLike",expertNameLike);
        }
        return UnifiedResponse.ok(findExpertCommonService.findAppExpertDTOByMulitCondition(params,start,pageSize));
    }

}
